package Friday.th5;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;

public class notebook extends JFrame implements ActionListener {
    //菜单
    JMenuBar jmb;
    JMenu jb;
    JMenuItem jb1,jb2;
    JTextArea jta;//文本区
    JScrollPane jsp;//滚动面板
    File file_edit=null;//当前打开的文件的文件名
    public static void main(String[] args) {
        new notebook();
    }
    public notebook(){
        jmb=new JMenuBar();
        this.setJMenuBar(jmb);
        jb=new JMenu("文件");
        jmb.add(jb);
        jb1=new JMenuItem("打开文件");
        jb2=new JMenuItem("保存文件");
        jb.add(jb1);
        jb.add(jb2);
        //为打开文件按钮和保存文件按钮注册监视器
        jb1.addActionListener(this);
        jb2.addActionListener(this);
        jta=new JTextArea();
        jsp=new JScrollPane(jta);
        this.add(jsp);

        //设置窗体属性
        setTitle("记事本");
        this.setSize(700,500);
        this.setLocation(300,300);
        this.setVisible(true);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
    }
    //打开文件操作
    public void openfile(){
        File fileName=null;
        //创建初始不可见的有模式的文件对话框
        JFileChooser jfc=new JFileChooser();
        //设置打开文件对话框的标题
        jfc.setDialogTitle("请选择文件");
        //默认方式打开对话框，返回一个int的整数
        int select_id= jfc.showOpenDialog(null);

        if(JFileChooser.APPROVE_OPTION ==select_id)
        {
            //得到用户选择的文件路径
            fileName=jfc.getSelectedFile().getAbsoluteFile();
            file_edit=fileName;
            if( (fileName ==null) || (!fileName.exists()) || (jta ==null) )
            {
                return;
            }
            InputStreamReader read =null;//底层输入流
            BufferedReader buffer=null;//上层输入流
            try
            {
                read =new InputStreamReader(new FileInputStream(fileName));
                buffer=new BufferedReader(read);
                String s=null;
                while ((s=buffer.readLine()) !=null)
                {
                    jta.append(s+"\r\n");
                }
            } catch (Exception e)
            {
                e.printStackTrace( );
                // TODO: handle exception
            }
            finally
            {
                try {
                    read.close();
                    buffer.close();
                } catch (IOException e)
                {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return ;
        }
        else if(select_id == JFileChooser.CANCEL_OPTION)
        {
            System.out.println("退出文件选择框");
            return;
        }
    }
    //保存文件
    public void savefile(){
        if ( (file_edit ==null)   )
        {
            return;
        }
        //如果文件不存在，则创建
        if ( !file_edit.exists() )
        {
            try
            {
                file_edit.createNewFile();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if (!file_edit.isFile()) {
            return;
        }
        FileWriter writer=null;
        BufferedWriter buffer=null;
        try
        {
            writer=new FileWriter(file_edit);//底层输出流
            buffer=new BufferedWriter(writer);//上层输出流
            //将文本存于字符串s中
            String[] s =this.jta.getText().split("\n");
            for (int i = 0; i < s.length; i++) {
                buffer.write(s[i] +"\r\n");
                buffer.flush();//清空缓冲区
            }
        } catch (Exception e)
        {
            e.printStackTrace( );
            // TODO: handle exception
        }
        finally
        {
            try
            {
                //先关闭上层流在关闭底层流
                buffer.close();
                writer.close();
            } catch (IOException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    @Override
    public void actionPerformed(ActionEvent actionEvent) {
        if(actionEvent.getSource().equals(jb1)){
            openfile();
            return ;
        }
        else if(actionEvent.getSource().equals(jb2)){
            savefile();
            return ;
        }
    }
}
